home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWArcShp.cpp next >
Encoding:
Text File  |  1995-11-08  |  7.5 KB  |  265 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWArcShp.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWARCSHP_H
  13. #include "FWArcShp.h"
  14. #endif
  15.  
  16. #ifndef FWOVLSHP_H
  17. #include "FWOvlShp.h"
  18. #endif
  19.  
  20. #ifndef FWRASTER_H
  21. #include "FWRaster.h"
  22. #endif
  23.  
  24. // ----- Foundation Includes -----
  25.  
  26. #ifndef FWSTREAM_H
  27. #include "FWStream.h"
  28. #endif
  29.  
  30. // ----- C Includes -----
  31.  
  32. #if defined(FW_BUILD_MAC) & !defined(__FP__)
  33. #include <FP.h>
  34. #endif
  35.  
  36. #ifdef FW_BUILD_WIN
  37. #include <math.h>
  38. #endif
  39.  
  40. //========================================================================================
  41. //    RunTime Info
  42. //========================================================================================
  43.  
  44. #if FW_LIB_EXPORT_PRAGMAS
  45. #pragma lib_export on
  46. #endif
  47.  
  48. #ifdef FW_BUILD_MAC
  49. #pragma segment fwgraphxshape
  50. #endif
  51.  
  52. FW_DEFINE_CLASS_M1(FW_CArcShape, FW_CBoundedShape)
  53.  
  54. FW_REGISTER_ARCHIVABLE_CLASS(FW_LArcShape, FW_CArcShape, FW_CArcShape::Read, FW_CShape::Write)
  55.  
  56. //========================================================================================
  57. //    class FW_CArcShape
  58. //========================================================================================
  59.  
  60. //----------------------------------------------------------------------------------------
  61. //    FW_CArcShape::FW_CArcShape
  62. //----------------------------------------------------------------------------------------
  63.  
  64. FW_CArcShape::FW_CArcShape(const FW_CArcShape& other) :
  65.     FW_CBoundedShape(other),
  66.     fStartAngle(other.fStartAngle),
  67.     fArcAngle(other.fArcAngle)
  68. {
  69.     FW_END_CONSTRUCTOR
  70. }
  71.  
  72. //----------------------------------------------------------------------------------------
  73. //    FW_CArcShape::FW_CArcShape
  74. //----------------------------------------------------------------------------------------
  75.  
  76. FW_CArcShape::FW_CArcShape(const FW_CRect& rect, 
  77.                             short startAngle,
  78.                             short arcAngle,
  79.                             FW_ERenderVerbs renderVerb,
  80.                             const FW_PInk& ink,
  81.                             const FW_PStyle& style) :
  82.     FW_CBoundedShape(rect, renderVerb, ink, style, FW_kNormalFont),
  83.     fStartAngle(startAngle),
  84.     fArcAngle(arcAngle)
  85. {
  86.     FW_END_CONSTRUCTOR
  87. }
  88.  
  89. //----------------------------------------------------------------------------------------
  90. //    FW_CArcShape::FW_CArcShape
  91. //----------------------------------------------------------------------------------------
  92.  
  93. FW_CArcShape::FW_CArcShape(FW_CReadableStream& archive) :
  94.     FW_CBoundedShape(archive)
  95. {
  96.     archive >> fStartAngle;
  97.     archive >> fArcAngle;
  98.  
  99.     FW_END_CONSTRUCTOR
  100. }
  101.  
  102. //----------------------------------------------------------------------------------------
  103. //    FW_CArcShape::~FW_CArcShape
  104. //----------------------------------------------------------------------------------------
  105.  
  106. FW_CArcShape::~FW_CArcShape()
  107. {
  108.     FW_START_DESTRUCTOR
  109. }
  110.  
  111. //----------------------------------------------------------------------------------------
  112. //    FW_CArcShape::operator=
  113. //----------------------------------------------------------------------------------------
  114.  
  115. FW_CArcShape& FW_CArcShape::operator=(const FW_CArcShape& other)
  116. {
  117.     if (this != &other)
  118.     {
  119.         FW_CBoundedShape::operator=(other);
  120.     
  121.         fStartAngle = other.fStartAngle;
  122.         fArcAngle = other.fArcAngle;
  123.     }
  124.     
  125.     return *this;
  126. }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. //    FW_CArcShape::Render
  130. //----------------------------------------------------------------------------------------
  131.  
  132. void FW_CArcShape::Render(FW_CGraphicContext& gc) const
  133. {    
  134.     gc.GetRasterizer()->RenderArc(gc,
  135.                                     fRect,
  136.                                     fStartAngle, fArcAngle,
  137.                                     GetRenderVerb(),
  138.                                     fInk,
  139.                                     fStyle);
  140. }
  141.  
  142. //----------------------------------------------------------------------------------------
  143. //    FW_CArcShape::RenderArc
  144. //----------------------------------------------------------------------------------------
  145.  
  146. void FW_CArcShape::RenderArc(FW_CGraphicContext& gc,
  147.                                 const FW_CRect& rect, 
  148.                                 short startAngle, short arcAngle,
  149.                                 FW_ERenderVerbs renderVerb, 
  150.                                 const FW_PInk& ink, 
  151.                                 const FW_PStyle& style)
  152. {    
  153.     gc.GetRasterizer()->RenderArc(gc,
  154.                                     rect,
  155.                                     startAngle, arcAngle,
  156.                                     renderVerb,
  157.                                     ink,
  158.                                     style);
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. //    FW_CArcShape::CalcAngle
  163. //----------------------------------------------------------------------------------------
  164. //    Need to be changed. Could do better than that.
  165.  
  166. short FW_CArcShape::CalcAngle(const FW_CPoint& test) const
  167. {
  168.     FW_Double width = (fRect.right - fRect.left).AsDouble();
  169.     FW_Double height = (fRect.bottom - fRect.top).AsDouble();
  170.     
  171.     FW_CFixed fxZero = FW_IntToFixed(0);
  172.     
  173.     if (test.y == fxZero || height == 0)
  174.         return test.x < fxZero ? 270 : 90;
  175.  
  176.     if (test.x == fxZero || width == 0)
  177.         return test.y < fxZero ? 180 : 0;
  178.     
  179.     FW_Double arc = atan(test.y.AsDouble() * height / test.x.AsDouble() * width);
  180. //    FW_Double arc = atan2((FW_Double)test.x * width, (FW_Double)test.y * height);
  181.     
  182.     arc = 90.0 - ((180.0 * arc) / 3.1415926536);
  183.     
  184.     return arc;
  185. }
  186.  
  187. //----------------------------------------------------------------------------------------
  188. //    FW_CArcShape::HitTest
  189. //----------------------------------------------------------------------------------------
  190.  
  191. FW_Boolean FW_CArcShape::HitTest(FW_CGraphicContext& gc,
  192.                                  const FW_CPoint& test,
  193.                                  FW_CFixed tolerance) const
  194. {
  195.     if(fRenderVerb == FW_kNoRendering)
  196.         return FALSE;
  197.  
  198.     if (FW_CBoundedShape::HitTest(gc, test, tolerance))
  199.     {
  200.         short arcAngle = CalcAngle(test) - fStartAngle;
  201.         if (((arcAngle & 0x8000) == (fArcAngle & 0x8000)) &&    // same sign
  202.             FW_Absolute((long)fArcAngle) >= FW_Absolute((long)arcAngle))     
  203.         {
  204.             FW_COvalShape oval(fRect, fRenderVerb);
  205.             return oval.HitTest(gc, test, tolerance);
  206.         }
  207.     }
  208.  
  209.     return FALSE;
  210. }
  211.  
  212. //----------------------------------------------------------------------------------------
  213. //    FW_CArcShape::Copy
  214. //----------------------------------------------------------------------------------------
  215.  
  216. FW_CShape* FW_CArcShape::Copy() const
  217. {
  218.     return FW_NEW(FW_CArcShape, (*this));
  219. }
  220.  
  221. //----------------------------------------------------------------------------------------
  222. //    FW_CArcShape::GetGeometry
  223. //----------------------------------------------------------------------------------------
  224.  
  225. void FW_CArcShape::GetGeometry(FW_CRect& rect, short& startAngle, short& arcAngle) const
  226. {
  227.     rect = fRect;
  228.     startAngle = fStartAngle;
  229.     arcAngle = fArcAngle;
  230. }
  231.  
  232. //----------------------------------------------------------------------------------------
  233. //    FW_CArcShape::SetGeometry
  234. //----------------------------------------------------------------------------------------
  235.  
  236. void FW_CArcShape::SetGeometry(const FW_CRect& rect, short startAngle, short arcAngle)
  237. {
  238.     fRect = rect;
  239.     fStartAngle = startAngle;
  240.     fArcAngle = arcAngle;
  241. }
  242.  
  243. //----------------------------------------------------------------------------------------
  244. //    FW_CArcShape::Flatten
  245. //----------------------------------------------------------------------------------------
  246.  
  247. void FW_CArcShape::Flatten(FW_CWritableStream& archive) const
  248. {
  249.     FW_CBoundedShape::Flatten(archive);
  250.     archive << fStartAngle;
  251.     archive << fArcAngle;
  252. }
  253.  
  254. //----------------------------------------------------------------------------------------
  255. //    FW_CArcShape::Read
  256. //----------------------------------------------------------------------------------------
  257.  
  258. void* FW_CArcShape::Read(FW_CReadableStream& archive)
  259. {
  260.     return FW_NEW(FW_CArcShape, (archive));
  261. }
  262.  
  263.  
  264.  
  265.